home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / amiga / gui / prcgntn1.lha / Precognition / source / slider.c < prev    next >
C/C++ Source or Header  |  1992-12-23  |  5KB  |  240 lines

  1. #include <stdio.h>
  2. #include "minmax.h"
  3. #include "Slider.h"
  4. #include "SliderClass.h"
  5. #include "EmbossedGadgetClass.h"
  6. #include <proto/exec.h>
  7. #include <proto/intuition.h>
  8. #include <proto/graphics.h>
  9. #include "amigamem.h"
  10.  
  11.  
  12. /* Slider 'state' setings. */
  13. #define WAITING    0
  14. #define DRAGGING   1
  15.  
  16.  
  17.  
  18. #ifdef BUILDER
  19.  
  20. Slider *Slider_New( Slider *self )
  21. {
  22.    Slider *new_slider = NULL;
  23.  
  24.    if (new_slider = (Slider *) Amalloc(sizeof(Slider)))
  25.    {
  26.       Slider_Init( new_slider,   self->eg.Location.x, self->eg.Location.y,
  27.                    self->eg.Size.x, self->eg.Size.y,
  28.                    self->eg.Pens );
  29.    }
  30.    return new_slider;
  31. }
  32.  
  33. #endif
  34.  
  35. void Slider_CleanUp( Slider *self )
  36. {
  37.    self->eg.g.SpecialInfo = NULL;
  38.  
  39.    Afree(self->eg.BoxBorder);
  40.    self->eg.BoxBorder = NULL; 
  41. }
  42.  
  43.  
  44.  
  45. void Slider_Render( Slider *self, RastPort *RPort )
  46. {
  47.    DrawBorder( RPort, &self->eg.BoxBorder->TopLeft,
  48.       self->eg.Location.x, self->eg.Location.y );
  49. }
  50.  
  51.  
  52. void Slider_Refresh( Slider *self )
  53. {
  54.    struct pcgWindow *window;
  55.  
  56.    window = InteractorWindow( self );
  57.  
  58.    Render( self, window->Window->RPort );
  59.    EmbossedGadget_Refresh( self );
  60. }
  61.  
  62.  
  63.  
  64. #define X_BORDER_WIDTH 2
  65. #define Y_BORDER_WIDTH 1
  66.  
  67. tPoint Slider_SetLocation(  Slider *self,
  68.                             PIXELS  LeftEdge,
  69.                             PIXELS  TopEdge )
  70. {
  71.    Forbid(); /* Don't want Intuition looking at these while we modify em. */
  72.    self->eg.g.LeftEdge  = LeftEdge + X_BORDER_WIDTH * 2;
  73.    self->eg.g.TopEdge   = TopEdge  + Y_BORDER_WIDTH * 2;
  74.  
  75.    self->eg.Location.x  = LeftEdge;
  76.    self->eg.Location.y  = TopEdge;
  77.  
  78.    Permit();
  79.  
  80.    return self->eg.Location;
  81. }
  82.  
  83.  
  84.  
  85. tPoint Slider_AskSize( Slider *self,
  86.                        PIXELS  Width,
  87.                        PIXELS  Height )
  88. {
  89.    tPoint size;
  90.  
  91.    size.x = MAX( Width,  10 );
  92.    size.y = MAX( Height, 10 );
  93.  
  94.    return size;
  95. }
  96.  
  97. tPoint Slider_SetSize( Slider *self,
  98.                        PIXELS  Width,
  99.                        PIXELS  Height )
  100. {
  101.    tPoint size;
  102.  
  103.    size = AskSize( self, Width, Height );
  104.  
  105.  
  106.    Forbid(); /* Don't want Intuition looking at these while we modify em. */
  107.    self->eg.Size     = size;
  108.    self->eg.g.Width  = size.x - X_BORDER_WIDTH * 4;
  109.    self->eg.g.Height = size.y - Y_BORDER_WIDTH * 4;
  110.  
  111.    pcg_Init3DBox(  self->eg.BoxBorder, 0, 0, size.x, size.y,
  112.                    self->eg.Pens.BrightPen,  self->eg.Pens.DarkPen,
  113.                    self->eg.BoxBorder->BottomRight.NextBorder );
  114.  
  115.    AlignText( &self->eg.LabelText, Size(self) );
  116.  
  117.    Permit();
  118.    return size;
  119. }
  120.  
  121.  
  122.  
  123.  
  124. BOOL Slider_elaborated = FALSE;
  125.  
  126. struct PositionerClass Slider_Class;
  127.  
  128. void SliderClass_Init( struct PositionerClass *class )
  129. {
  130.    PositionerClass_Init( class );
  131.    EmbossedGadgetClass_Init( class );
  132.    class->isa         = PositionerClass();
  133.    class->ClassName   = "Slider";
  134.    class->CleanUp     = Slider_CleanUp;
  135.    class->SetLocation = Slider_SetLocation;
  136.    class->SetSize     = Slider_SetSize;
  137.    class->AskSize     = Slider_AskSize;
  138.    class->Render      = Slider_Render;
  139.    class->Refresh     = Slider_Refresh;
  140.    class->Activate    = NULL;
  141.    class->isActive    = NULL;
  142. }
  143.  
  144.  
  145. struct PositionerClass *SliderClass( void )
  146. {
  147.    if (! Slider_elaborated)
  148.    {
  149.       SliderClass_Init( &Slider_Class );
  150.       Slider_elaborated = TRUE;
  151.    }
  152.  
  153.    return &Slider_Class;
  154. }
  155.  
  156.  
  157. #define V36_PROPNEWLOOK 0x0010
  158.    /*
  159.    ** This flag turns on the 3D look of the autoknob under Workbench 2.0
  160.    ** It's ignored under 1.3
  161.    **
  162.    ** I've redefined it here so that I can compile under 1.3 too.
  163.    */
  164.  
  165. void Slider_Init( Slider    *self,
  166.                   PIXELS     LeftEdge,
  167.                   PIXELS     TopEdge,
  168.                   PIXELS     Width,
  169.                   PIXELS     Height,
  170.                   pcg_3DPens Pens )
  171. {
  172.    Point size;
  173.  
  174.  
  175.    EmbossedGadget_Init( self, LeftEdge, TopEdge, Width, Height,
  176.       GADGHCOMP, GADGIMMEDIATE | RELVERIFY | FOLLOWMOUSE, PROPGADGET, Pens, NULL );
  177.  
  178.    size = Slider_AskSize( self, Width, Height );
  179.  
  180.    self->eg.isa = SliderClass();
  181.    self->eg.Size = size;
  182.  
  183.    self->eg.g.LeftEdge         = LeftEdge + X_BORDER_WIDTH*2;
  184.    self->eg.g.TopEdge          = TopEdge  + Y_BORDER_WIDTH*2;
  185.    self->eg.g.Width            = Width    - X_BORDER_WIDTH*4;
  186.    self->eg.g.Height           = Height   - Y_BORDER_WIDTH*4;
  187.    self->eg.g.GadgetRender     = (APTR) &(self->AutoKnob);
  188.    self->eg.g.SelectRender     = NULL;
  189.    self->eg.g.SpecialInfo      = (APTR) &(self->Prop);
  190.  
  191.    self->Prop.Flags       = AUTOKNOB | PROPBORDERLESS | V36_PROPNEWLOOK;
  192.    self->Prop.HorizPot    = 0x8000;
  193.    self->Prop.VertPot     = 0x8000;
  194.    self->Prop.HorizBody   = 0xFFFF;
  195.    self->Prop.VertBody    = 0xFFFF;
  196.    self->Prop.CWidth      = 0;
  197.    self->Prop.CHeight     = 0;
  198.    self->Prop.HPotRes     = 0;
  199.    self->Prop.VPotRes     = 0;
  200.    self->Prop.LeftBorder  = 0;
  201.    self->Prop.TopBorder   = 0;
  202.  
  203.    SetLocation( self, LeftEdge, TopEdge );
  204.    SetSize    ( self, Width, Height );
  205. }
  206.  
  207.  
  208. void SlideLower( Slider *self )
  209. {
  210.    long    position;
  211.    USHORT   delta, value;
  212.  
  213.    delta = KnobSize( self ) /2;
  214.    delta = MAX( delta, 1 );
  215.  
  216.    value = Value(self);
  217.    position = value - delta;
  218.    position = MAX( position, 0 );
  219.  
  220.    SetValue( self, position );
  221. }
  222.  
  223.  
  224. void SlideHigher( Slider *self )
  225. {
  226.    ULONG  position;
  227.    USHORT delta;
  228.    USHORT value;
  229.  
  230.    delta = KnobSize( self ) /2;
  231.    delta = MAX( delta, 1 );
  232.    value = Value(self);
  233.  
  234.    position = value + delta;
  235.    position = MIN( position, 0xFFFF );
  236.  
  237.    SetValue( self, position );
  238. }
  239.  
  240.